<?php
//Ce script permet de lister les workflows, il permet aussi de pauser un enregistrement avant s'il faut
header('Content-Type: text/xml; charset=UTF-8');
require_once("../../config.php");
require_once("myLib.php");
require_once("PlistParser.inc");
global $CFG, $COURSE, $USER;

//Paramètres
$Ginstid = optional_param('instid', null, PARAM_INT);
$Guuid = optional_param('uuid', null, PARAM_SAFEDIR);
$Gcam = optional_param('cam', null, PARAM_ALPHAEXT);

$doc = new DOMDocument('1.0', 'utf-8');
$message = $doc->createElement( "workflows" );
$doc->appendChild( $message );
$success = 'SUCCESS';
$reason = "";

$xmlResp = $doc->createElement( "response" );
//Permission
if(isset($Ginstid)){
    if(is_allowed('mod/podcast:edit', $COURSE->id, $Ginstid)){
        //On tente de mettre la caméra en pause, mais on accorde aucune importance à la réussite de l'opération.
        if($Gcam){
            $command1 = "ssh -i ".$CFG->podcast_path2rsakey." ".$CFG->podcast_username."@".$CFG->podcast_path2pcp." pcast --server ".$CFG->podcast_path2pcp." --user ".$CFG->podcast_username." --pass ".$CFG->podcast_password." --pause \\'".$Gcam."\\'";
            $response =  exec($command1, $output, $return);
        }
        $command2 = "ssh -i ".$CFG->podcast_path2rsakey." ".$CFG->podcast_username."@".$CFG->podcast_path2pcp." pcast --server ".$CFG->podcast_path2pcp." --user ".$CFG->podcast_username." --pass ".$CFG->podcast_password." --listworkflows";
        $output =  shell_exec($command2);

        parseWorkflows($doc, $message, $output);
        
    }else{
        $success = 'Permission denied.' ;
    }
}else{
    $success = 'Permission denied.' ;
}

$resp = $doc->createElement( "response" );
$resp->appendChild( $doc->createTextNode( $success ));
$message->appendChild( $resp );
if (isset($Gcam) && isset($Ginstid)) {
    $xmlRespCameraName = $doc->createElement( "cameraName" );
    $xmlRespCameraUuid = $doc->createElement( "cameraUuid" );
    $xmlRespCameraName->appendChild( $doc->createTextNode( $Gcam ));
    $xmlRespCameraUuid->appendChild( $doc->createTextNode( $Guuid ));
    $message->appendChild( $xmlRespCameraName );
    $message->appendChild( $xmlRespCameraUuid );
}

echo $doc->saveXML();
?>